﻿Imports DevExpress.XtraGrid.Views.Grid
Public Class frmcategory
    Dim Strsql As String
    Dim _Mysql As New ClsSQLhelper
    Dim On_Load As Boolean = True
    Dim Primary_Key As Integer = -1
    Sub Load_Data()
        Try

            Dim DT As New DataTable

            Strsql = "select category_id,category_name,note,active,sectionid from category where  sectionid = " & CboSection.SelectedValue & " order by active desc ,category_name"
            DT = _Mysql.GetMYSQLDataTable(Strsql, "category")
            GridControl1.DataSource = DT
        Catch ex As Exception

            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "Load_Data"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try

    End Sub
    Sub Load_ComboboxData_section()
        Try
            Dim DS As New DataSet

            Strsql = "select * from section where active = 1 and  section_id <>  0 order by section_name "
            Me.EventLog1.WriteEntry(Me.Name & " : " & Strsql, EventLogEntryType.Information, 1)
            DS = _Mysql.GetMYSQLDataset(Strsql, "DS_section", "section")
            ' Me.EventLog1.WriteEntry(Strsql, EventLogEntryType.Information)
            With Me.CboSection
                If DS.Tables(0).Rows.Count = 0 Then Exit Sub
                .DataSource = DS.Tables(0)
                .DisplayMember = "section_name"
                .ValueMember = "section_id"
            End With

        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "Load_ComboboxData_section"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try
    End Sub

    Private Sub frmcategory_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Load_ComboboxData_section()

        Load_Data()
        On_Load = False


    End Sub

   




    'Private Sub BtnNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
    '    Me.Txtid.Text = ""
    '    Me.Txtname.Text = ""
    '    Me.TxtNote.Text = ""
    '    Me.ChkActive.Checked = False
    'End Sub


    'Private Sub BtnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
    '    Try
    '        If Me.Txtname.Text.Trim = "" Then Exit Sub
    '        If MsgBox("คุณต้องการบันทึกข้อมูลหรือไม่", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "ยืนยันการบันทึก") = MsgBoxResult.No Then Exit Sub

    '        Dim DS As New DataSet
    '        If Me.Txtid.Text = "" Then
    '            Strsql = "Select * from category where category_name= '" & Me.Txtname.Text & "'  and sectionid = " & Me.CboSection.SelectedValue
    '            Me.EventLog1.WriteEntry(Me.Name & " : " & Strsql, EventLogEntryType.Information, 1)
    '            DS = Mysql1.GetMYSQLDataset(Strsql, "DS_Category", "Category")
    '            If DS.Tables(0).Rows.Count <> 0 Then
    '                MsgBox("Category นี้มีอยู่แล้วไม่สามารถบันทึกซ้ำได้อีก", MsgBoxStyle.Critical)
    '                Exit Sub
    '            End If
    '            Strsql = "Insert into category (category_name,note,sectionid) values ('" & Me.Txtname.Text & "', '" & Me.TxtNote.Text & "'," & Me.CboSection.SelectedValue & ")"
    '            Me.EventLog1.WriteEntry(Me.Name & " : " & Strsql, EventLogEntryType.Information, 1)
    '            Mysql1.MySQLExecute(Strsql)
    '            MsgBox("เพิ่มข้อมูลเข้าเรียบร้อยแล้ว", MsgBoxStyle.Information)
    '        Else

    '            Strsql = "update category set category_name= '" & Me.Txtname.Text & "', note = '" & Me.TxtNote.Text & "' ,active = " & Me.ChkActive.Checked & " , sectionid = " & Me.CboSection.SelectedValue
    '            Strsql = Strsql & " where category_id = " & Me.Txtid.Text.Trim
    '            Me.EventLog1.WriteEntry(Me.Name & " : " & Strsql, EventLogEntryType.Information, 1)
    '            Mysql1.MySQLExecute(Strsql)
    '            MsgBox("ปรับปรุงข้อมูลเข้าเรียบร้อยแล้ว", MsgBoxStyle.Information)
    '        End If
    '        'Refresh Data
    '        Load_Data(Me.CboSite.SelectedValue, Me.CboSection.SelectedValue)
    '        BtnNew_Click(sender, e)

    '    Catch ex As Exception
    '        Me.EventLog1.WriteEntry(Me.Name & " : " & ex.Message, EventLogEntryType.Error, 1)
    '        MsgBox(ex.Message())
    '    End Try
    'End Sub



    Private Sub BtnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnCancel.Click
        Me.Dispose()
    End Sub


    Private Sub CboSection_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CboSection.SelectedIndexChanged
        If On_Load <> True Then Load_Data()
    End Sub


  
    Private Sub GridView1_CustomRowCellEditForEditing(ByVal sender As System.Object, ByVal e As DevExpress.XtraGrid.Views.Grid.CustomRowCellEditEventArgs) Handles GridView1.CustomRowCellEditForEditing
        Try
            Dim Gv As GridView = sender
            Primary_Key = Gv.GetRowCellValue(e.RowHandle, Gv.Columns("category_id"))

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub


    Private Sub BtnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnDelete.Click
        Try
            If Primary_Key = -1 Then Exit Sub
            Strsql = "Select * from category where category_id = " & Primary_Key
            Dim DT As DataTable
            DT = _Mysql.GetMYSQLDataTable(Strsql)
            If DT.Rows(0).Item("readonly") = True Then
                MsgBox("ขออภัยคุณไม่สามารถลบ category นี้ได้เนื่องจากเป็นค่าตั้งต้น", MsgBoxStyle.Exclamation, "ไม่สามารถทำรายการได้")
                Exit Sub
            End If

            If MsgBox("คุณต้องการลบข้อมูลหรือไม่", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "ยืนยันการลบ") = MsgBoxResult.No Then Exit Sub
            Strsql = "Delete from category where category_id = " & Primary_Key

            _Mysql.MySQLExecute(Strsql)
            'Refresh Data
            Load_Data()
        Catch ex As Exception

            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "BtnDelete_Click"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try
    End Sub

    Private Sub BtnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnAdd.Click
        With frmNewCategory
            .Section_ID = Me.CboSection.SelectedValue
            If .ShowDialog() = Windows.Forms.DialogResult.OK Then
                Load_Data()
            End If
        End With
    End Sub

    Private Sub BtnRefresh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnRefresh.Click
        Load_Data()
    End Sub

    Private Sub BtnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnEdit.Click
        If Primary_Key = -1 Then Exit Sub
        Strsql = "Select * from category where category_id = " & Primary_Key
        Dim DT As DataTable
        DT = _Mysql.GetMYSQLDataTable(Strsql)
        If DT.Rows(0).Item("readonly") = True Then
            MsgBox("ขออภัยคุณไม่สามารถลบ category นี้ได้เนื่องจากเป็นค่าตั้งต้น", MsgBoxStyle.Exclamation, "ไม่สามารถทำรายการได้")
            Exit Sub
        End If
        With frmEditCategorys
            .Primary_Key = Primary_Key
            .Section_ID = Me.CboSection.SelectedValue
            If .ShowDialog() = Windows.Forms.DialogResult.OK Then
                Load_Data()
            End If
        End With
    End Sub

    Private Sub GridControl1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GridControl1.Click

    End Sub
End Class